package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.SanWar.VO.ApplyForExchangeVo;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.ApplyForExchange;
import com.example.wm.SanWar.entity.Pointsrecord;
import com.example.wm.SanWar.entity.PrizeManagement;
import com.example.wm.SanWar.mapper.ApplyForExchangeMapper;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.ApplyForExchangeService;
import com.example.wm.SanWar.service.PointsrecordService;
import com.example.wm.SanWar.service.PrizeManagementService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.SanWar.VO.PrizeManagementfindVo;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/ApplyForExchange")
public class ApplyForExchangeContoller {
    @Resource
    private ApplyForExchangeService applyForExchangeService;
    @Resource
    private ApplyForExchangeMapper applyForExchangeMapper;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private AlliesandUserService alliesandUserService;
    @Resource
    private PrizeManagementService prizeManagementService;
    @Resource
    private PointsrecordService pointsrecordService;
    @PostMapping("/Insert")
    public Result Insert(@RequestBody ApplyForExchange applyForExchange){

        applyForExchange.setApplytime(tsyUtils.getnowtime());
        //查询库存
        LambdaQueryWrapper<PrizeManagement> lq=new LambdaQueryWrapper<>();
        lq.eq(PrizeManagement::getId,applyForExchange.getApplyraffleid());
        PrizeManagement prizeManagement = prizeManagementService.getOne(lq);
        //查询脸蛋值和积分
        LambdaQueryWrapper<AlliesandUser> lq2=new LambdaQueryWrapper<>();
        lq2.eq(AlliesandUser::getId,applyForExchange.getApplyid());
        AlliesandUser alliesandUser = alliesandUserService.getOne(lq2);
        if(prizeManagement.getStorenumber()>=applyForExchange.getRafflenumber()){
            //处理库存
            prizeManagement.setStorenumber(prizeManagement.getStorenumber()- applyForExchange.getRafflenumber());
            prizeManagementService.updateById(prizeManagement);
            //处理脸蛋值和积分

            int number=prizeManagement.getPoints()*applyForExchange.getRafflenumber();
            int faces=prizeManagement.getFundraisingfaces()*applyForExchange.getRafflenumber();
            alliesandUser.setPointes(String.valueOf(Integer.parseInt(alliesandUser.getPointes())-number));
            alliesandUser.setFaces(String.valueOf(Integer.parseInt(alliesandUser.getFaces())-faces));
            alliesandUserService.updateById(alliesandUser);
             //插入积分记录表 扣除
            if(number>0){
                Pointsrecord pointsrecord=new Pointsrecord();
                pointsrecord.setRewordpoepleid(alliesandUser.getId());
                pointsrecord.setRewordpoeple(alliesandUser.getName());
                pointsrecord.setCreatetime(tsyUtils.getnowtime());
                pointsrecord.setCreatename(alliesandUser.getName());
                pointsrecord.setCreatenameid(alliesandUser.getId());
                pointsrecord.setType("积分兑换");
                pointsrecord.setType2("可用积分");
                pointsrecord.setStatuss("扣减");
                pointsrecord.setPoints(number);
                pointsrecord.setCreatappoint(alliesandUser.getAppoint());
                pointsrecord.setCompetitionseason(alliesandUser.getCompetitionseason());
                pointsrecord.setRewardorreducedetail("兑换奖品《"+prizeManagement.getRafflename()+"》X"+applyForExchange.getRafflenumber());
                pointsrecord.setFinnalpoints(Integer.parseInt(alliesandUser.getPointes()));
                pointsrecordService.save(pointsrecord);
            }
            //处理积分记录表 扣除 脸蛋
            if(faces>0){
                Pointsrecord pointsrecord=new Pointsrecord();
                pointsrecord.setRewordpoepleid(alliesandUser.getId());
                pointsrecord.setRewordpoeple(alliesandUser.getName());
                pointsrecord.setCreatetime(tsyUtils.getnowtime());
                pointsrecord.setCreatename(alliesandUser.getName());
                pointsrecord.setCreatenameid(alliesandUser.getId());
                pointsrecord.setCompetitionseason(alliesandUser.getCompetitionseason());
                pointsrecord.setFinnalpoints(Integer.parseInt(alliesandUser.getFaces()));
                pointsrecord.setType("募捐脸蛋");
                pointsrecord.setType2("脸蛋");
                pointsrecord.setStatuss("扣减");
                pointsrecord.setPoints(faces);
                pointsrecord.setCreatappoint(alliesandUser.getAppoint());
                pointsrecord.setRewardorreducedetail("兑换奖品《"+prizeManagement.getRafflename()+"》X"+applyForExchange.getRafflenumber());
                pointsrecordService.save(pointsrecord);
            }




            applyForExchangeMapper.insert(applyForExchange);

            return ResultData.success(1);


        }else{
            return ResultData.error(203,"库存不足");

        }
    }
    @PostMapping("/update")
    public Result update(@RequestBody ApplyForExchange applyForExchange){
        System.out.println("applyForExchange:"+applyForExchange.toString());

        if(applyForExchange.getStatus().equals("未通过") || applyForExchange.getStatus().equals("待发货")){
            applyForExchange.setChecktime(tsyUtils.getnowtime());
            if(applyForExchange.getStatus().equals("未通过")){
                //更新库存
                LambdaQueryWrapper<PrizeManagement> lq=new LambdaQueryWrapper<>();
                lq.eq(PrizeManagement::getId,applyForExchange.getApplyraffleid());
                PrizeManagement prizeManagement = prizeManagementService.getOne(lq);
                prizeManagement.setStorenumber(prizeManagement.getStorenumber()+applyForExchange.getRafflenumber());
                prizeManagementService.updateById(prizeManagement);
                //更新积分
                //查询脸蛋值和积分
                LambdaQueryWrapper<AlliesandUser> lq2=new LambdaQueryWrapper<>();
                lq2.eq(AlliesandUser::getId,applyForExchange.getApplyid());
                AlliesandUser alliesandUser = alliesandUserService.getOne(lq2);
                //处理脸蛋值和积分
                int number=prizeManagement.getPoints()*applyForExchange.getRafflenumber();
                int faces=prizeManagement.getFundraisingfaces()*applyForExchange.getRafflenumber();
                alliesandUser.setPointes(String.valueOf(Integer.parseInt(alliesandUser.getPointes())+number));
                alliesandUser.setFaces(String.valueOf(Integer.parseInt(alliesandUser.getFaces())+faces));
                alliesandUserService.updateById(alliesandUser);

                //插入积分记录表 奖励

                if(number>0){
                    Pointsrecord pointsrecord=new Pointsrecord();
                    pointsrecord.setRewordpoepleid(alliesandUser.getId());
                    pointsrecord.setRewordpoeple(alliesandUser.getName());
                    pointsrecord.setCreatetime(tsyUtils.getnowtime());
                    pointsrecord.setCreatename(alliesandUser.getName());
                    pointsrecord.setCreatenameid(alliesandUser.getId());
                    pointsrecord.setType("积分兑换");
                    pointsrecord.setType2("可用积分");
                    pointsrecord.setStatuss("奖励");
                    pointsrecord.setPoints(number);
                    pointsrecord.setCreatappoint(alliesandUser.getAppoint());
                    pointsrecord.setCompetitionseason(alliesandUser.getCompetitionseason());
                    pointsrecord.setRewardorreducedetail("兑换奖品《"+prizeManagement.getRafflename()+"》X"+applyForExchange.getRafflenumber()
                    +"被驳回，可用积分退还"
                    );
                    pointsrecord.setFinnalpoints(Integer.parseInt(alliesandUser.getPointes()));
                    pointsrecordService.save(pointsrecord);
                }
                //处理积分记录表  脸蛋
                if(faces>0){
                    Pointsrecord pointsrecord=new Pointsrecord();
                    pointsrecord.setRewordpoepleid(alliesandUser.getId());
                    pointsrecord.setRewordpoeple(alliesandUser.getName());
                    pointsrecord.setCreatetime(tsyUtils.getnowtime());
                    pointsrecord.setCreatename(alliesandUser.getName());
                    pointsrecord.setCreatenameid(alliesandUser.getId());
                    pointsrecord.setCompetitionseason(alliesandUser.getCompetitionseason());
                    pointsrecord.setFinnalpoints(Integer.parseInt(alliesandUser.getFaces()));
                    pointsrecord.setType("募捐脸蛋");
                    pointsrecord.setType2("脸蛋");
                    pointsrecord.setStatuss("奖励");
                    pointsrecord.setPoints(faces);
                    pointsrecord.setCreatappoint(alliesandUser.getAppoint());
                    pointsrecord.setRewardorreducedetail("兑换奖品《"+prizeManagement.getRafflename()+"》X"+applyForExchange.getRafflenumber()
                            +"被驳回，脸蛋退还"
                    );
                    pointsrecordService.save(pointsrecord);
                }

            }
        }

        if(applyForExchange.getStatus().equals("待收货")){
            applyForExchange.setDeliverytime(tsyUtils.getnowtime());
        }
        if(applyForExchange.getStatus().equals("已完成")){
            applyForExchange.setReceipttime(tsyUtils.getnowtime());
        }
        return ResultData.success(applyForExchangeService.updateById(applyForExchange));
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody ApplyForExchange applyForExchange){
        return ResultData.success(applyForExchangeService.removeById(applyForExchange));
    }
    //限购查询
    @RequestMapping("/find")
    public Result find(@RequestBody JSONObject jsonObject){
        int   purchaseresnumber= jsonObject.getInteger("purchaseresnumber");//限购数量
        int  rafflenumber= jsonObject.getInteger("rafflenumber");//申请数量
        String competitionseason= jsonObject.getString("competitionseason");//赛季
        String ispurchaserestriction= jsonObject.getString("ispurchaserestriction");//是否限购
        if(Objects.equals(ispurchaserestriction, "个人限购")){
            LambdaQueryWrapper<ApplyForExchange> lq=new LambdaQueryWrapper<>();
            lq.eq(ApplyForExchange::getApplyid,jsonObject.getString("applyid"));
            lq.ne(ApplyForExchange::getStatus,"未通过");
            lq.ne(ApplyForExchange::getStatus,"未中奖");
            lq.eq(ApplyForExchange::getApplyraffleid,jsonObject.getString("applyraffleid"));
            List<ApplyForExchange> applyForExchangeList = applyForExchangeService.list(lq);
            Boolean isExist=    isExist(purchaseresnumber,rafflenumber,applyForExchangeList);
            return ResultData.success(isExist);
        }else if(Objects.equals(ispurchaserestriction, "赛季限购")){
            LambdaQueryWrapper<ApplyForExchange> lq=new LambdaQueryWrapper<>();
            lq.eq(ApplyForExchange::getCompetitionseason,competitionseason);
            lq.eq(ApplyForExchange::getApplyid,jsonObject.getString("applyid"));
            lq.eq(ApplyForExchange::getApplyraffleid,jsonObject.getString("applyraffleid"));
            lq.ne(ApplyForExchange::getStatus,"未通过");
            lq.ne(ApplyForExchange::getStatus,"未中奖");
            List<ApplyForExchange> applyForExchangeList = applyForExchangeService.list(lq);
           Boolean isExist=    isExist(purchaseresnumber,rafflenumber,applyForExchangeList);
            return ResultData.success(isExist);
        }else if(Objects.equals(ispurchaserestriction, "按月限购")){
            LambdaQueryWrapper<ApplyForExchange> lq=new LambdaQueryWrapper<>();
            lq.eq(ApplyForExchange::getApplyraffleid,jsonObject.getString("applyraffleid"));
            lq.ne(ApplyForExchange::getStatus,"未通过");
            lq.ne(ApplyForExchange::getStatus,"未中奖");
            // 获取当前日期
            LocalDate givenDate = LocalDate.now();
            YearMonth yearMonth = YearMonth.from(givenDate);
            LocalDate firstDayOfMonth = yearMonth.atDay(1);
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
            lq.between(ApplyForExchange::getApplytime, firstDayOfMonth, lastDayOfMonth);
            List<ApplyForExchange> applyForExchangeList = applyForExchangeService.list(lq);
            System.out.println("当前月份的起始日期：" + firstDayOfMonth+"  "+lastDayOfMonth);
            Boolean isExist=    isExist(purchaseresnumber,rafflenumber,applyForExchangeList);
            return ResultData.success(isExist);
        }else if(Objects.equals(ispurchaserestriction, "不限购")){
            return ResultData.success(true);
        }
        return ResultData.success(true);
    }
    private  boolean isExist(int purchaseresnumber,int rafflenumber,List<ApplyForExchange> applyForExchangeList){
        int number=0;
        if(!applyForExchangeList.isEmpty()){
            for (ApplyForExchange aly : applyForExchangeList){
                number+=aly.getRafflenumber();
            }

         System.out.println("number:"+number+"rafflenumber:"+rafflenumber);
            return number + rafflenumber <= purchaseresnumber;

        }else {
            return true;
        }
    }
    @PostMapping("/findbyid")
    public Result findbyid(@RequestBody ApplyForExchange applyForExchange){
        LambdaQueryWrapper<ApplyForExchange> lq1=new LambdaQueryWrapper<>();
        lq1.eq(ApplyForExchange::getApplyraffleid,applyForExchange.getApplyraffleid());
        lq1.eq(ApplyForExchange::getStatus,"待审核");
        List<ApplyForExchange> applyForExchangeList = applyForExchangeMapper.selectList(lq1);

        LambdaQueryWrapper<ApplyForExchange> lq2=new LambdaQueryWrapper<>();
        lq2.eq(ApplyForExchange::getApplyraffleid,applyForExchange.getApplyraffleid());
        lq2.ne(ApplyForExchange::getStatus,"待审核");
        lq2.ne(ApplyForExchange::getStatus,"未通过");
        lq2.ne(ApplyForExchange::getStatus,"未中奖");
        List<ApplyForExchange> applyForExchangeList2 = applyForExchangeMapper.selectList(lq2);
        int number=0;
        if(!applyForExchangeList2.isEmpty()){
            for (ApplyForExchange aly : applyForExchangeList2){
                number+= aly.getRafflenumber();
            }
        }

              PrizeManagementfindVo pzv=new PrizeManagementfindVo();
               pzv.setNumberdone(number);
               pzv.setNumber(applyForExchangeList2.size());
               pzv.setApplyForExchangeList(applyForExchangeList2);
               pzv.setApplynumber(applyForExchangeList.size());

      return   ResultData.success(pzv);
    }

    @PostMapping("/findbyapplyraffleid")
    public Result findbyapplyraffleid(@RequestBody JSONObject jsonObject){

        Page<ApplyForExchange> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<ApplyForExchange> lq2=new LambdaQueryWrapper<>();
        lq2.eq(ApplyForExchange::getApplyraffleid,jsonObject.getInteger("applyraffleid"));
        if(jsonObject.getString("status").equals("待审核")){
            lq2.eq(ApplyForExchange::getStatus,"待审核");

        }else{
            //待发货
            if(jsonObject.getString("status").equals("待发货")){
                lq2.eq(ApplyForExchange::getStatus,"待发货");

            }else{
                lq2.ne(ApplyForExchange::getStatus,"待审核");

            }

        }
        lq2.ne(ApplyForExchange::getStatus,"未通过");
        lq2.ne(ApplyForExchange::getStatus,"未中奖");
//        List<ApplyForExchange> applyForExchangeList2 = applyForExchangeMapper.selectList(lq2);
        Page<ApplyForExchange>  applyForExchangeList2=applyForExchangeService.page(pageInfo,lq2);
        Page<ApplyForExchangeVo>  list2=new Page<>();
        list2.setPages(applyForExchangeList2.getPages());
        list2.setTotal(applyForExchangeList2.getTotal());
        list2.setCurrent(applyForExchangeList2.getCurrent());
        list2.setSize(applyForExchangeList2.getSize());
//        ArrayList<ApplyForExchangeVo>  list2=new ArrayList<>();

        ArrayList<ApplyForExchangeVo> applyForExchangeVos=new ArrayList<>();
        if(applyForExchangeList2.getSize()>0){
          for (ApplyForExchange aly : applyForExchangeList2.getRecords()){
              ApplyForExchangeVo applyForExchangeVo=new ApplyForExchangeVo();
              applyForExchangeVo.setApplyForExchange(aly);
              applyForExchangeVo.setPrizeManagement(prizeManagementService.getById(aly.getApplyraffleid()));

              LambdaQueryWrapper<AlliesandUser> lq=new LambdaQueryWrapper<>();
              lq.eq(AlliesandUser::getId,aly.getApplyid());
              AlliesandUser one = alliesandUserService.getOne(lq);
              applyForExchangeVo.setApplyname(one.getName());
              applyForExchangeVo.setFoework(one.getFoework());
              applyForExchangeVo.setPoints(one.getPointes());

              applyForExchangeVos.add(applyForExchangeVo);


              list2.setRecords(applyForExchangeVos);
          }
        }
        return   ResultData.success(list2);
    }

    @PostMapping("/findtoexcel")
    public Result findtoexcel(@RequestBody JSONObject jsonObject) throws IOException {
        LambdaQueryWrapper<ApplyForExchange> lq2=new LambdaQueryWrapper<>();
        lq2.eq(ApplyForExchange::getApplyraffleid,jsonObject.getInteger("applyraffleid"));
        lq2.eq(ApplyForExchange::getStatus,"待发货");
        List<ApplyForExchange> applyForExchangeList2 = applyForExchangeMapper.selectList(lq2);
        ArrayList<ApplyForExchangeVo> applyForExchangeVos=new ArrayList<>();
        if(applyForExchangeList2.size()>0){
            for (ApplyForExchange aly : applyForExchangeList2){
                ApplyForExchangeVo applyForExchangeVo=new ApplyForExchangeVo();
                applyForExchangeVo.setApplyForExchange(aly);
                applyForExchangeVo.setPrizeManagement(prizeManagementService.getById(aly.getApplyraffleid()));
                LambdaQueryWrapper<AlliesandUser> lq=new LambdaQueryWrapper<>();
                lq.eq(AlliesandUser::getId,aly.getApplyid());
                AlliesandUser one = alliesandUserService.getOne(lq);
                applyForExchangeVo.setApplyname(one.getName());
                applyForExchangeVo.setFoework(one.getFoework());
                applyForExchangeVo.setPoints(one.getPointes());
                applyForExchangeVos.add(applyForExchangeVo);
            }
        }
        // 创建Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("兑换申请");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        Cell headerCell = headerRow.createCell(0);
        headerCell.setCellValue("申请人");
        headerCell = headerRow.createCell(1);
        headerCell.setCellValue("申请物品");
        headerCell = headerRow.createCell(2);
        headerCell.setCellValue("申请数量");
        headerCell = headerRow.createCell(3);
        headerCell.setCellValue("收货人姓名");
        headerCell = headerRow.createCell(4);
        headerCell.setCellValue("电话号码");
        headerCell = headerRow.createCell(5);
        headerCell.setCellValue("地址");

         if(!applyForExchangeVos.isEmpty())
         {
             // 填充数据
        int rowNum = 1;
        for (ApplyForExchangeVo vo : applyForExchangeVos) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(2).setCellValue(vo.getApplyForExchange().getRafflenumber());
            row.createCell(1).setCellValue(vo.getPrizeManagement().getRafflename());
            row.createCell(0).setCellValue(vo.getApplyname());
            row.createCell(3).setCellValue(vo.getApplyForExchange().getReceivepeople());
            row.createCell(4).setCellValue(vo.getApplyForExchange().getPhonenumber());
            row.createCell(5).setCellValue(vo.getApplyForExchange().getAddress());

        }

         }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);

        return   ResultData.success(baos.toByteArray());
    }
    @PostMapping("/findbymyselfpage")
    private Result findbymyself(@RequestBody JSONObject jsonObject){
        LambdaQueryWrapper<ApplyForExchange> lq=new LambdaQueryWrapper<>();
        lq.eq(ApplyForExchange::getApplyid,jsonObject.getString("applyid"));
        lq.eq(ApplyForExchange::getStatus,jsonObject.getString("status"));
        Page<ApplyForExchange> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        Page<ApplyForExchange> applyForExchangeList = applyForExchangeService.page(pageInfo,lq);
        Page<ApplyForExchangeVo> applyForExchangeVoPage=new Page<>();
        applyForExchangeVoPage.setPages(applyForExchangeList.getPages());
        applyForExchangeVoPage.setTotal(applyForExchangeList.getTotal());
        applyForExchangeVoPage.setCurrent(applyForExchangeList.getCurrent());
        applyForExchangeVoPage.setSize(applyForExchangeList.getSize());
        ArrayList<ApplyForExchangeVo> applyForExchangeVos=new ArrayList<>();
        if (applyForExchangeList.getSize()>0){
            for (ApplyForExchange aly : applyForExchangeList.getRecords()){
                ApplyForExchangeVo af=new ApplyForExchangeVo();

                LambdaQueryWrapper<AlliesandUser> lq1=new LambdaQueryWrapper<>();
                lq1.eq(AlliesandUser::getId,aly.getApplyid());
                AlliesandUser one = alliesandUserService.getOne(lq1);
                af.setApplyname(one.getName());
                af.setApplyForExchange(aly);
                LambdaQueryWrapper<PrizeManagement> lq2=new LambdaQueryWrapper<>();
                lq2.eq(PrizeManagement::getId,aly.getApplyraffleid());
                PrizeManagement prizeManagement = prizeManagementService.getOne(lq2);
                af.setPrizeManagement(prizeManagement);
                applyForExchangeVos.add(af);


            }
            applyForExchangeVoPage.setRecords(applyForExchangeVos);

        }
        return ResultData.success(applyForExchangeVoPage);
    }


    @PostMapping("/findallalliasname")
    private Result findallalliasname(@RequestBody JSONObject jsonObject) {
        // 获取请求参数
        Integer applyid = jsonObject.getInteger("applyid");
        Long page = jsonObject.getLong("page");
        Long pageSize = jsonObject.getLong("pageSize");
        String status = jsonObject.getString("status");

        // 创建查询条件
        LambdaQueryWrapper<PrizeManagement> lq0 = new LambdaQueryWrapper<>();
        lq0.eq(PrizeManagement::getCreatenameid, applyid);
        List<PrizeManagement> prizeManagementList = prizeManagementService.list(lq0);

        // 创建分页对象
        Page<ApplyForExchangeVo> applyForExchangeVoPage = new Page<>(page, pageSize);
        ArrayList<ApplyForExchangeVo> allApplyForExchangeVos = new ArrayList<>();

        // 如果 prizeManagementList 为空，直接返回空的分页对象
        if (prizeManagementList == null || prizeManagementList.isEmpty()) {
            applyForExchangeVoPage.setTotal(0);
            applyForExchangeVoPage.setRecords(new ArrayList<>());
            return ResultData.success(applyForExchangeVoPage);
        }

        // 遍历 prizeManagementList
        for (PrizeManagement pm : prizeManagementList) {
            // 创建查询条件
            LambdaQueryWrapper<ApplyForExchange> lq = new LambdaQueryWrapper<>();
            lq.eq(ApplyForExchange::getApplyraffleid, pm.getId());
            lq.eq(ApplyForExchange::getStatus, status);

            // 查询兑换申请列表
            List<ApplyForExchange> applyForExchangeList = applyForExchangeService.list(lq);

            // 遍历查询结果
            for (ApplyForExchange aly : applyForExchangeList) {
                ApplyForExchangeVo af = new ApplyForExchangeVo();

                // 查询用户信息
                LambdaQueryWrapper<AlliesandUser> lq1 = new LambdaQueryWrapper<>();
                lq1.eq(AlliesandUser::getId, aly.getApplyid());
                AlliesandUser one = alliesandUserService.getOne(lq1);
                if (one != null) {
                    af.setApplyname(one.getName());
                }

                // 设置兑换申请信息
                af.setApplyForExchange(aly);

                // 查询奖品管理信息
                LambdaQueryWrapper<PrizeManagement> lq2 = new LambdaQueryWrapper<>();
                lq2.eq(PrizeManagement::getId, aly.getApplyraffleid());
                PrizeManagement prizeManagement = prizeManagementService.getOne(lq2);
                if (prizeManagement != null) {
                    af.setPrizeManagement(prizeManagement);
                }

                // 添加到结果列表
                allApplyForExchangeVos.add(af);
            }
        }

        // 设置分页信息
        applyForExchangeVoPage.setTotal(allApplyForExchangeVos.size());

        // 计算分页结果
        Long offset = (page - 1) * pageSize;
        Long toIndex = Math.min(offset + pageSize, allApplyForExchangeVos.size());
        List<ApplyForExchangeVo> pagedRecords = allApplyForExchangeVos.subList(offset.intValue(), toIndex.intValue());

        // 设置分页记录
        applyForExchangeVoPage.setRecords(pagedRecords);

        // 返回结果
        return ResultData.success(applyForExchangeVoPage);
    }

}

